(function(angular) {
	'use strict';
	angular.module("fs.waf").factory("custAcctService", custAcctService);
	custAcctService.$inject = [ '$q', 'loggerFactory', 'commonService', 'ajaxServiceFactory', 'addressInfoService' ];
	function custAcctService($q, loggerFactory, commonService, ajaxServiceFactory, addressInfoService) {
		var log = loggerFactory.getLogger("custAcctService"), ajaxService = ajaxServiceFactory.buildAjaxService({
			getDataOptions : {
				url : 'do/cda/custAcct/dropDownDataOptions',
				method : 'get'
			},
			getSearchCustAcctOptions : {
				url : 'do/cda/custAcct/searchCustAcctOptions',
				method : 'get'
			}
		});
		
		function initSearchOptions(){
			var defer = $q.defer();
			ajaxService.getDataOptions().then(function(response) {
				var object = {};
				object.paycodeCatOptions = response.data.paycodeCatOptions;
				object.custAcctTypeOptions = response.data.custAcctTypeOptions;
				object.ccyOptions = response.data.ccyOptions;
				defer.resolve(object);
			});
			return defer.promise;
		}
		
		function initOptions(){
			var defer = $q.defer();
			ajaxService.getDataOptions().then(function(response) {
				var object = {};
				object.custAcctTypeOptions = {};
				object.ccyOptions = {};
				object.allPartyTypeOptions = {};
				object.yesNoOptions = {};
				object.swiftCaseList = {};
				
				object.custAcctTypeOptions.data = response.data.custAcctTypeOptions;
				object.ccyOptions.data = response.data.ccyOptions;
				object.paycodeCatOptions = response.data.paycodeCatOptions;
				object.allPartyTypeOptions= response.data.allPartyTypeOptions;
				object.yesNoOptions.data = response.data.yesNoOptions;
				object.yesNoOptionsb = response.data.yesNoOptions;
				object.swiftCaseList = response.data.swiftCaseList;
				
				// get accountPatterns
				object.accountPatterns = [];
				object.accountPatternsTwo = [];
				if(response.data.accountPatterns){
					var s = [];
					angular.forEach(response.data.accountPatterns, function(data,
							index, array) {
						if (s.indexOf(data.value) === -1) {
							s.push(data.value);
						}
					});
					if (s.length > 0) {
						for (var i = 0; i < s.length; i++) {
							object.accountPatterns.push({
								"value" : '{'+s[i]+'}',
								"label" : s[i]
							});
						};
						for (var j = 0; j < s.length; j++) {
							object.accountPatternsTwo.push({
								"value" : '{'+s[j]+'}',
								"label" : s[j]
							});
						};
					};
				}
				
				
				addressInfoService.initData().then(function(result){
					object.partyCountrys = result.countryOptionAll;  //typeahead
					object.partyStates = result.stateOptionAll;
					object.partyCitys = result.cityOptionAll;
					object.stateList = result.stateList;
					object.cityList = result.cityList;
					object.partyStatesCatch = result.stateOptionAll;
					object.partyCitysCatch = result.cityOptionAll;
					defer.resolve(object);
				});
			});
			return defer.promise;
		}
		
		function initFunction(ctrl) {
			ctrl.partyCountryChanged = function(payParty){
				var partyStates = addressInfoService.getStateOptions(ctrl.stateList, payParty.partyCountry);
				var partyCitys = addressInfoService.getCityOptions(ctrl.cityList, payParty.partyCountry, payParty.partyState);
				if(partyStates && partyStates.length > 0){
					ctrl.partyStates = partyStates;
				}else{
					ctrl.partyStates = ctrl.partyStatesCatch;
				}
				if(partyCitys && partyCitys.length > 0){
					ctrl.partyCitys = partyCitys;
				}else{
					ctrl.partyCitys = ctrl.partyCitysCatch;
				}
			};
			
			ctrl.partyStateChanged = function(payParty){
				var partyCitys = addressInfoService.getCityOptions(ctrl.cityList, payParty.partyCountry, payParty.partyState);
				if(partyCitys && partyCitys.length > 0){
					ctrl.partyCitys = partyCitys;
				}else{
					ctrl.partyCitys = ctrl.partyCitysCatch;
				}
			};
			ctrl.acctNo_Active = 'Y';
			ctrl.paycodeCatChanged = function(custAcct){
				custAcct = custAcct || ctrl.custAcct;
				angular.forEach(ctrl.paycodeCatOptions, function(data, index){
					if(data.value ==  custAcct.paycodeCat ){
						if(data.extProperty == 'N'){
							ctrl.acctNo_Active = 'N';
						}else{
							ctrl.acctNo_Active = 'Y';
						}
					}
				});
				if(custAcct.paycodeCat != custAcct.siteCode + '-SWIFT'){
					ctrl.custAcctSwiftCatch = custAcct.custAcctSwift;
					custAcct.custAcctSwift = undefined;
				}else{
					custAcct.custAcctSwift = ctrl.custAcctSwiftCatch || custAcct.custAcctSwift || {payPartyList : []};
				}
			};
			ctrl.payPartyListCache = ctrl.payPartyListCache || {};
			ctrl.swiftTypeCallBack = function(swiftCase){
				if(swiftCase){
					ctrl.custAcct.custAcctSwift.caseCode = swiftCase.caseCode;
					ctrl.custAcct.custAcctSwift.swiftType = swiftCase.swiftType;
				}
				angular.forEach(ctrl.swiftCaseList, function(data, index, array){
					if(data.caseCode == ctrl.custAcct.custAcctSwift.caseCode && data.swiftType == ctrl.custAcct.custAcctSwift.swiftType){
						ctrl.custAcct.custAcctSwift.hasCoverMsg = data.globalSwiftCase.hasCoverMsg;
						if(!ctrl.custAcct.custAcctSwift.coverMsgReceiver || !commonService.isValidOption(ctrl.allPartyTypeOptions, ctrl.custAcct.custAcctSwift.coverMsgReceiver)){
							ctrl.custAcct.custAcctSwift.coverMsgReceiver = data.globalSwiftCase.coverMsgReceiver;
						}
						if(ctrl.custAcct.custAcctSwift.hasCoverMsg == 'N'){
							ctrl.custAcct.custAcctSwift.coverMsgReceiver = '';
						}
					}
				});
				ctrl.payPartyTabs = [];
				ctrl.payPartyConfig = [];
				var caseCodes = [];
				if(ctrl.custAcct.custAcctSwift.caseCode){
					if(ctrl.custAcct.custAcctSwift.caseCode.indexOf(";") != -1){
						caseCodes = ctrl.custAcct.custAcctSwift.caseCode.split(";");
					}else{
						caseCodes[0] = ctrl.custAcct.custAcctSwift.caseCode;
					}
				}
				
				angular.forEach(caseCodes, function(caseCode, index, array){
					angular.forEach(ctrl.allPartyTypeOptions, function(allPaypartype, key) {
						if(allPaypartype.value == caseCode && allPaypartype.extProperty == 'N'){
							ctrl.payPartyTabs.push(allPaypartype);
						}
					});
				});
				ctrl.coverMsgReceiverOptions = ctrl.allPartyTypeOptions;
				if(swiftCase){//OK event
					angular.forEach(ctrl.payPartyTabs, function(data, index, array){
						ctrl.custAcct.custAcctSwift.payPartyList[index] = ctrl.payPartyListCache[data.value] || {};
						ctrl.payPartyListCache[data.value] = ctrl.custAcct.custAcctSwift.payPartyList[index];
						ctrl.custAcct.custAcctSwift.payPartyList[index].partyType = data.value;
						ctrl.payPartyConfig[index] = angular.copy(ctrl.layoutConfig.formConfigs.payParty);
					});
				}else{//init page
					angular.forEach(ctrl.payPartyTabs, function(data, index, array){
						ctrl.custAcct.custAcctSwift.payPartyList[index] = ctrl.custAcct.custAcctSwift.payPartyList[index] || {};
						ctrl.payPartyListCache[data.value] = ctrl.custAcct.custAcctSwift.payPartyList[index];
						ctrl.custAcct.custAcctSwift.payPartyList[index].partyType = data.value;
						ctrl.payPartyConfig[index] = angular.copy(ctrl.layoutConfig.formConfigs.payParty);
					});
				}
			};
			
			ctrl.checkPayPartyList = function(){
				if(ctrl.payPartyTabs && ctrl.custAcct.custAcctSwift){
					ctrl.custAcct.custAcctSwift.payPartyList.splice(ctrl.payPartyTabs.length);
				}
			};
			
			ctrl.initPyPartyTabs = function(){
				if(ctrl.custAcct.custAcctSwift && ctrl.custAcct.custAcctSwift.caseCode && ctrl.custAcct.custAcctSwift.swiftType){
					ctrl.swiftTypeCallBack();
				}
				ctrl.paycodeCatChanged();
			};
		}
		
		
		
		function partyTypeInList(selPaypartype,payPartyList){
			var flag = false;
			angular.forEach(payPartyList, function(payParty, key) {
				if(selPaypartype.value == payParty.partyType){
					flag = true;
				}
			});
			return flag;
		}
		
		function partyTypeInOption(selPaypartype,payPartyList){
			var flag = false;
			angular.forEach(payPartyList, function(payParty, key) {
				if(selPaypartype.partyType == payParty.value){
					flag = true;
				}
			});
			return flag;
		}
		
		return {
			initOptions : initOptions,
			initFunction : initFunction,
			initSearchOptions : initSearchOptions
		};

	}
})(angular);
